<?php
/**
 * Created by JetBrains PhpStorm.
 * User: dodasso
 * Date: 7/14/13
 * Time: 8:10 PM
 * To change this template use File | Settings | File Templates.
 */

define('APP_ROOT', realpath(__DIR__ . '/../..'));

require_once(APP_ROOT . '/modules/persistance/DBConnector.php');
require_once(APP_ROOT . '/modules/common/Errors.php');
require_once(APP_ROOT . '/modules/session/SessionModule.php');
require_once(APP_ROOT . '/modules/inventory/InventoryModule.php');

use persistance\DBConnector;
use common\Errors;
use common\SessionModule;
use inventory\InventoryModule;

function testListInventoryObjects() {
    $dbconn = new DBConnector();
    $dbconn->connect();
    $smod = new SessionModule();
    $smod->init($dbconn);
    $rs = $smod->login('admin', 'admin', 'localhost', 'none');
    $json = json_decode($rs, true);
    if((is_null($json)) ||
        ($json['errorCode'] != Errors::INTEGRAL_ERR_NONE)) {
        print('testListMeasuringUnits(): FAIL' . PHP_EOL);
        return;
    }
    unset($rs);

    $sessionKey = $json['data']['sessionKey'];
    $imod = new InventoryModule();
    $imod->init($dbconn, $smod);
    $rs = $imod->listInventoryObjects();
    $json = json_decode($rs, true);
    if((is_null($json)) ||
        ($json['errorCode'] != Errors::INTEGRAL_ERR_NONE)) {
        print('testListInventoryObjects(): FAIL' . PHP_EOL);
        $smod->logout($sessionKey);
        return;
    }

    print('testListInventoryObjects(): OK' . PHP_EOL);

    $smod->logout($sessionKey);
}

function testFindInventoryObject() {
    $cmod = new CommonModule();
    $rs = $cmod->login('admin', 'admin', 'localhost', 'none');
    $json = json_decode($rs, true);
    if((is_null($json)) ||
        ($json['errorCode'] != Errors::INTEGRAL_ERR_NONE)) {
        print('testFindInventoryObject(): FAIL' . PHP_EOL);
        return;
    }
    unset($rs);

    $sessionKey = $json['data']['sessionKey'];
    $imod = new InventoryModule();
    $imod->initWithSessionKey($sessionKey);
    $rs = $imod->findInventoryObjectById(1);
    $json = json_decode($rs, true);
    if((is_null($json)) ||
        ($json['errorCode'] != Errors::INTEGRAL_ERR_NONE)) {
        print('testFindInventoryObject(): FAIL' . PHP_EOL);
        $cmod->logout($sessionKey);
        return;
    }

    print('testFindInventoryObject(): OK' . PHP_EOL);

    $cmod->logout($sessionKey);
}

DBConnector::connect();

testListInventoryObjects();
testFindInventoryObject();

DBConnector::disconnect();